package io.zcoder.innoway.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.zcoder.innoway.R;
import io.zcoder.innoway.utils.Utils;
import io.zcoder.innoway.commons.Constants;
import io.zcoder.innoway.widgets.EditTextCustom;
import io.zcoder.innoway.widgets.TextViewCustom;

public class InputPassCodeActivity extends BaseActivity {

    public static final int RESPONSE_SET_PASS_CODE = 222;
    public static final int RESPONSE_CONFIRM_PASS_CODE = 333;
    public static final int TYPE_SET_PASS_CODE = 1;
    public static final int TYPE_CHANGE_PASS_CODE = 2;
    public static final int TYPE_CONFIRM_PASS_CODE = 3;

    @OnClick(R.id.common_cancel_right)
    void cancel() {
        finish();
    }

    @BindView(R.id.common_cancel_right)
    TextViewCustom mCancel;
    @BindView(R.id.title_pass_code)
    TextViewCustom mTitlePassCode;
    @BindView(R.id.input_pass_code)
    EditTextCustom mInputPassCode;
    @BindViews({R.id.input_pass_code_1, R.id.input_pass_code_2, R.id.input_pass_code_3, R.id.input_pass_code_4, R.id.input_pass_code_5, R.id.input_pass_code_6})
    TextViewCustom[] mViewPassCode;

    private int mCountInput = 0;
    private String mPassCode1;
    private String mOldPassCode;
    private String mConfirmPassCode;
    private int mCurrentType = TYPE_SET_PASS_CODE;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_input_pass_code);
        ButterKnife.bind(this);
        mCancel.setVisibility(View.VISIBLE);

        mOldPassCode = getIntent().getStringExtra(Constants.PASS_CODE_EXTRAS);
        mConfirmPassCode = getIntent().getStringExtra(Constants.CONFIRM_PASS_CODE_EXTRAS);

        if (!Utils.isBlank(mOldPassCode)) {
            setTitle(getString(R.string.change_pass_code));
            mCurrentType = TYPE_CHANGE_PASS_CODE;
            mTitlePassCode.setText(getString(R.string.enter_your_old_passcode));
        } else if (!Utils.isBlank(mConfirmPassCode)) {
            setTitle(getString(R.string.turn_off_passcode));
            mCurrentType = TYPE_CONFIRM_PASS_CODE;
            mTitlePassCode.setText(getString(R.string.enter_your_passcode));
        } else {
            setTitle(getString(R.string.set_pass_code));
        }

        mInputPassCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String input = s.toString();
                for (int i = 0; i < mViewPassCode.length; i++) {
                    if (input.length() > i) {
                        mViewPassCode[i].setText(input.charAt(i) + "");
                    } else {
                        mViewPassCode[i].setText("");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == mViewPassCode.length) {
                    if (mCurrentType == TYPE_SET_PASS_CODE) {
                        if (mCountInput == 0) {
                            mPassCode1 = mInputPassCode.getText().toString();
                            mTitlePassCode.setText(R.string.verify_your_new_passcode);
                            mInputPassCode.setText("");
                            mCountInput++;
                        } else if (mCountInput == 1 && s.toString().equalsIgnoreCase(mPassCode1)) {
                            Intent intent = new Intent();
                            intent.putExtra(Constants.PASS_CODE_EXTRAS, mPassCode1);
                            setResult(RESPONSE_SET_PASS_CODE, intent);
                            finish();
                        } else {
                            mInputPassCode.setText("");
                            showAlert(getString(R.string.error), getString(R.string.error_passcode_verify));
                        }
                    } else if (mCurrentType == TYPE_CHANGE_PASS_CODE) {
                        if (mCountInput == 0) {
                            if (s.toString().equalsIgnoreCase(mOldPassCode)) {
                                mCountInput++;
                                mInputPassCode.setText("");
                                mTitlePassCode.setText(R.string.enter_your_new_passcode);
                            } else {
                                showAlert(getString(R.string.error), getString(R.string.error_passcode));
                                mInputPassCode.setText("");
                            }
                        } else if (mCountInput == 1) {
                            mPassCode1 = mInputPassCode.getText().toString();
                            mTitlePassCode.setText(R.string.verify_your_new_passcode);
                            mInputPassCode.setText("");
                            mCountInput++;
                        } else if (mCountInput == 2 && s.toString().equalsIgnoreCase(mPassCode1)) {
                            Intent intent = new Intent();
                            intent.putExtra(Constants.PASS_CODE_EXTRAS, mPassCode1);
                            setResult(RESPONSE_SET_PASS_CODE, intent);
                            finish();
                        } else {
                            mInputPassCode.setText("");
                            showAlert(getString(R.string.error), getString(R.string.error_passcode_verify));
                        }
                    } else if (mCurrentType == TYPE_CONFIRM_PASS_CODE) {
                        if (s.toString().equalsIgnoreCase(mConfirmPassCode)) {
                            Intent intent = new Intent();
                            intent.putExtra(Constants.CONFIRM_PASS_CODE_EXTRAS, true);
                            setResult(RESPONSE_CONFIRM_PASS_CODE, intent);
                            finish();
                        } else {
                            showAlert(getString(R.string.error), getString(R.string.error_passcode));
                            mInputPassCode.setText("");
                        }
                    }
                }
            }
        });
    }

}
